<template>
    <div class="container">
        <div class="top">
            <div class="title h3">菜单配置管理</div>
        </div>
        <!-- :defaultExpandAll="true" -->
        <div class="bottom">
            <div class="bottom_left">
                <a-tree v-if="dataLoading" :treeData="dataList" :show-line="showLine" :show-icon="showIcon" :defaultExpandedKeys="['0-0']">
                    <template slot="custom" slot-scope="item">
                        <span>{{ item.name }}</span>
                        <div class="tree-action">
                            <a-icon title="添加菜单" type="plus" @click="(e)=> add(item)" />
                            <a-icon title="修改菜单" type="edit" @click="(e)=> edit(item)" />
                            <a-icon title="删除菜单" type="delete" @click="(e)=> remove(item)" />
                        </div>
                    </template>
                </a-tree>
            </div>
        </div>
        <!-- 添加弹框 -->
        <a-modal v-model="addPop" width="50%" title="新增菜单" centered @cancel="()=>form = {}" :maskClosable="false" destroyOnClose>
            <a-form-model ref="formRules" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol" :colon="false">
                <a-form-model-item label="父级菜单">
                    <a-input v-model="form.parentName" disabled />
                </a-form-model-item>
                <a-form-model-item label="菜单名称" prop="name">
                    <a-input v-model="form.name" />
                </a-form-model-item>
                <a-form-model-item label="菜单地址" prop="menuUrl">
                    <a-input v-model="form.menuUrl" />
                </a-form-model-item>
                <a-form-model-item label="是否启用">
                    <a-switch v-model="form.enable" />
                </a-form-model-item>
                <a-form-model-item label="菜单描述">
                    <a-input v-model="form.note" />
                </a-form-model-item>
            </a-form-model>

            <template slot="footer">
                <a-button type="primary" @click="SubmitAdd">确定</a-button>
            </template>
        </a-modal>
        <a-modal v-model="editPop" width="50%" title="编辑菜单" centered @cancel="()=>editForm = {}" :maskClosable="false" destroyOnClose>
            <a-form-model ref="editFromRule" :model="editForm" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol" :colon="false">
                <a-form-model-item label="父级菜单">
                    <a-tree-select v-model="editForm.parentId" :treeDefaultExpandAll="true" :dropdown-style="{ maxHeight: '200px', overflow: 'auto' }" :tree-data="treeList" :defaultValue="editForm.parentId" placeholder="请选择父级菜单" @change="onSelectTree" :replaceFields="replaceFields">
                    </a-tree-select>
                </a-form-model-item>
                <a-form-model-item label="菜单名称" prop="name">
                    <a-input v-model="editForm.name" />
                </a-form-model-item>
                <a-form-model-item label="菜单地址" prop="menuUrl">
                    <a-input v-model="editForm.menuUrl" />
                </a-form-model-item>
                <a-form-model-item label="是否启用">
                    <a-switch v-model="editForm.enable" />
                </a-form-model-item>
                <a-form-model-item label="菜单描述">
                    <a-input v-model="editForm.note" />
                </a-form-model-item>
            </a-form-model>

            <template slot="footer">
                <a-button type="primary" @click="SubmitEdit">确定</a-button>
            </template>
        </a-modal>
    </div>
</template>

<script>
export default {
    name: "menuList",

    data() {
        return {
            dataLoading: false,
            addPop: false,
            editPop: false,
            showLine: true,
            showIcon: true,
            dataList: [],
            replaceFields: {
                children: "children",
                title: "name",
                key: "id",
                value: "id",
            },
            page: {
                current: 1,
                size: 100000,
            },
            form: {
                parentId: -1,
                parentName: "",
                name: "",
                menuUrl: "",
                enable: true,
                note: "",
            },
            editForm: {
                id: "",
                parentId: "",
                parentName: "",
                name: "",
                menuUrl: "",
                enable: true,
                note: "",
            },
            treeList: [],
            labelCol: { span: 4 },
            wrapperCol: { span: 16 },
            rules:{
              name:[{required:true,message:"菜单名称必填",trigger:'blur'}],
              menuUrl:[{required:true,message:"菜单地址必填",trigger:'blur'}],
            }
        };
    },
    methods: {
        map2Ary(map) {
            var list = [];
            for (var key in map) {
                list.push([key, map[key]]);
            }
            return list;
        },
        //树状图
        onSelect(selectedKeys, info) {
            console.log("selected", selectedKeys, info);
        },
        toTree(data) {
            // 删除 所有 children,以防止多次调用
            data.forEach(function (item) {
                delete item.children;
            });

            // 将数据存储为 以 id 为 KEY 的 map 索引数据列
            var map = {};
            data.forEach(function (item) {
                map[item.id] = item;
            });
            // console.log(map);
            var val = [];
            data.forEach(function (item) {
                // 以当前遍历项，的pid,去map对象中找到索引的id
                var parent = map[item.parentId];
                // 好绕啊，如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
                item.scopedSlots = { title: "custom" };
                if (parent) {
                    item.parentName = parent.name;
                    (parent.children || (parent.children = [])).push(item);
                } else {
                    item.parentName = "根级菜单";
                    item.parentId = "-1";
                    //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
                    val.push(item);
                }
            });
            return val;
        },
        onSelectTree(value, label, extra) {
            console.log(value, label, extra);
        },
        getData() {
            this.$api.getMenuList(this.page).then((res) => {
                console.log(res);
                if (res.data.code == 0) {
                    //this.tableData = res.data.data;
                    //this.total = res.data.total;
                    let data = res.data.data;
                    let list = this.toTree(data);

                    let _data = [
                        {
                            children: list,
                            id: -1,
                            parentId: -1,
                            name: "根级菜单",
                            enable: true,
                            scopedSlots: { title: "custom" },
                        },
                    ];
                    this.treeList = _data;
                    this.dataList = _data;
                    this.dataLoading = true;
                } else {
                    this.$message.info(res.data.errorMessage);
                }
            });
        },
        add(item) {
            this.addPop = true;
            this.form.parentId = item.id;
            this.form.parentName = item.name;
            this.form.enable = true;
            console.log(item);
        },
        edit(item) {
            this.editPop = true;
            this.editForm.id = item.id;
            this.editForm.parentName = item.parentName;
            this.editForm.enable = item.enable;
            this.editForm.parentId = item.parentId;
            this.editForm.name = item.name;
            this.editForm.menuUrl = item.menuUrl;
            this.editForm.note = item.note;

            console.log(this.editForm);
        },
        remove(item) {
            let that = this;
            if (item.children && item.children.length > 0) {
                that.$message.info("此菜单下有子菜单不能删除!");
                return;
            }
            this.$confirm({
                title: "是否确认删除",
                onOk() {
                    that.$api.delMenu(item.id).then((res) => {
                        if (res.data.data == true) {
                            that.$message.info("数据删除成功!");
                            that.getData();
                        } else {
                            that.$message.info(res.data.errorMessage);
                        }
                    });
                },
                onCancel() {
                    console.log("Cancel");
                },
                class: "delk",
            });
        },
        SubmitEdit() {
          this.$refs.editFromRule.validate(val=>{
            if(val){
              this.$api.editMenu(this.editForm).then((res) => {
                console.log(res);
                if (res.data.code == 0) {
                  this.$message.info("修改成功!");
                  this.editPop = false;
                  this.editForm = {};
                  this.getData();
                }
              });
            }
          })

        },
        SubmitAdd() {
          this.$refs.formRules.validate(val=>{
            if(val){
              this.$api.addMenu(this.form).then((res) => {
                console.log(res);
                if (res.data.code == 0) {
                  this.$message.info("添加成功!");
                  this.addPop = false;
                  this.form = {};
                  this.getData();
                }
              });
            }
          })

        },
    },
    mounted() {
        this.getData();
    },
};
</script>

<style lang="scss" scoped>
.container {
    width: 100%;
    height: 100%;

    .top {
        padding-left: 30px;
        width: 100%;
        line-height: 50px;
        border-bottom: 1px solid #258cd7;
        box-sizing: border-box;
        display: flex;
        align-items: center;

        .title {
            font-size: 20px;
        }
    }

    .bottom {
        width: 100%;
        height: calc(100% - 50px);
        display: flex;

        .bottom_left {
            width: 40%;
            height: 98%;
            box-sizing: border-box;
            border-right: 1px solid #258cd7;
            color: #37ddc0;
            position: relative;
            overflow-y: auto;
            .btn-list {
                width: 100%;
                display: flex;
                justify-content: center;
                position: absolute;
                left: 0;
                bottom: 20px;

                div {
                    margin: 0 10px;
                }
            }
        }

        .bottom_right {
            width: 60%;
            height: 100%;
            display: flex;
            justify-content: space-between;
            padding: 20px;

            .map {
                width: 60%;
                height: 100%;
                border: 1px solid #37ddc0;
            }

            .datail {
                width: 38%;
                height: 100%;
                padding-top: 15px;
                position: relative;

                span {
                    color: #37ddc0;
                    font-size: 20px;
                }

                ul {
                    li {
                        margin: 20px 0;

                        div {
                            color: #9fb145;
                        }

                        color: rgb(204, 201, 201);
                    }
                }

                .btnrlist {
                    display: flex;
                    position: absolute;
                    bottom: 0;
                    right: 0;

                    div {
                        margin-left: 15px;
                    }
                }
            }
        }
    }
}

//按钮
.btn {
    display: flex;
    justify-content: center;
    width: 100px;
    padding: 5px 10px;
    border-radius: 5px;
    color: black;
}

.add {
    background: #2c6a5f;
}

.edit {
    background: #2e5882;
}

.del {
    background: #954a45;
}


::v-deep .ant-tree li .ant-tree-node-content-wrapper {
    font-size: 16px;
}

.ant-tree ::v-deep li {
    position: relative;
}
::v-deep .tree-action {
    position: absolute;
    top: 5px;
    right: 10px;
}
::v-deep .tree-action i {
    margin-left: 10px;
    color: #258cd7;
}
</style>